module.exports = function ($scope, $resource, $modal, FileUploader, toaster) {

	//查询机构基本信息
	$scope.loadBaseInfo = function(){
		$resource('/api/as/sc/office/getCompanyBaseInfo', {}, {}).save({}, function(res){
			if (res.errcode === 0) {
				$scope.company = res.data;
				$scope.company.company_name = $scope.company.name;
				$scope.company.company_code = $scope.company.code;
			}
		})
	}

	init();
	//查询当前机构是否已申报
	function init() {
		$scope.company = {};
		$scope.fromData = {
			'register': false
		}
		//无参数执行添加,查看当前机构申报状态
		$resource('/api/ac/goc/companyApplyService/getCheckStatus', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
				$scope.fromData.status = res.data;
				if(res.data == '0'){
					$scope.fromData.msg = '已申报参加本系统，无需再次申报。';
				} else if(res.data == '1'){
					$scope.fromData.msg = '审核已通过，可以申请出境游。';
				} else if(res.data == '2'){
					$scope.fromData.msg = '审核未通过，无法申请出境游，可重新申报。';
				} else {
					$scope.fromData.register = true;
					$scope.loadBaseInfo();
				}
            } else {
				$scope.fromData.register = false;
			}
        });
	}

	//企业申报
	$scope.ok = function() {
		if(!checkInfo($scope.company)) return;
		$scope.company.status = $scope.fromData.status;
		$resource('/api/ac/goc/companyApplyService/addCompanyForRoster', {}, {}).save($scope.company, function(res){
            if (res.errcode === 0 && res.data == '1') {
				$scope.fromData.register = false;
				$scope.fromData.status = '0'; //任意值都可
				$scope.fromData.msg = '申报成功, 请耐心等待审核结果.';
            } else {
                toaster.error({title: '', body: '申请失败, 请重试'});
            }
        });
	}

	//重新申报
	$scope.reApply = function() {
		$scope.fromData.register = true;
		$scope.loadBaseInfo();
	}


	$scope.uploader = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
	});
	
	//上传图片成
	$scope.uploader.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.company.license_img = response.savename;
	}

	//校验企业信息
	function checkInfo(company){
		if(!company.master){
			toaster.warning({title: '', body: '请填写负责人名称.'});
			return false;
		}
		if(!company.mobile){
			toaster.warning({title: '', body: '请填写负责人手机号'});
			return false;
		}
		if(!/^1[3|5|7|8|9]\d{9}$/.test(company.mobile)){
			toaster.warning({title: '', body: '手机号格式不正确'});
			return false;
		}
		if(!company.license_code){
			toaster.warning({title: '', body: '请填写执照编号.'});
			return false;
		}
		if(!company.license_img){
			toaster.warning({title: '', body: '请上传营业执照图片.'});
			return false;
		}
		return true;
	}

};